package com.pdg.islandinterloper.clientstatehandlers;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import com.pdg.islandinterloper.MainActivity;
import com.pdg.islandinterloper.R;
import com.pdg.islandinterloper.baseclasses.ClientStateHandlerBase;
import com.pdg.islandinterloper.database.objects.Profile;
import com.pdg.islandinterloper.enums.ClientState;

public class OpenProfileClientStateHandler extends ClientStateHandlerBase<MainActivity> implements DialogInterface.OnClickListener, OnItemSelectedListener{

	private ArrayAdapter<Profile> dataAdapter = null;
	private Profile selectedProfile = null;
	
	private ArrayAdapter<Profile> getDataAdapter(){
		return dataAdapter;
	}
	private void setDataAdapter(ArrayAdapter<Profile> theAdapter){
		dataAdapter = theAdapter;
	}
	
	public OpenProfileClientStateHandler(MainActivity theActivity) {
		super(theActivity);
	}

	@Override
	public void handleHide() {
	}

	@Override
	public void handleButtonClick(View theView) {
		switch(theView.getId()){
		case R.id.button_back:
			getActivity().setState(ClientState.PLAY);
			break;
		case R.id.button_delete_profile:
			showConfirmDeleteProfileDialog();
			break;
		case R.id.button_use_profile:
			getActivity().setProfileId(getSelectedProfile().getId());
			switch(getSelectedProfile().getProfileState()){
			case UNINITIALIZED:
				getActivity().setState(ClientState.UNINITIALIZED_PROFILE);
				break;
			default:
				break;
			}
			break;
		}
	}

	@Override
	public boolean handleMenu(MenuItem theItem) {
		return false;
	}
	
	private Spinner getSpinner(){
		return (Spinner)getActivity().findViewById(R.id.spinner_profiles);
	}

	@Override
	public void handleShow() {
		Spinner theSpinner = getSpinner();
		if(this!=theSpinner.getOnItemSelectedListener()){
			theSpinner.setOnItemSelectedListener(this);
		}
		refreshDataAdapter();
	}
	
	private void showConfirmDeleteProfileDialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		builder.setTitle(R.string.alert_title_confirm)
		.setMessage(R.string.alert_message_confirm_delete_profile)
		.setPositiveButton(R.string.button_confirm, this)
		.setNegativeButton(R.string.button_cancel, this);
		builder.show();
	}

	private void refreshDataAdapter() {
		Spinner theSpinner = getSpinner();
		setDataAdapter(new ArrayAdapter<Profile>(getActivity(), android.R.layout.simple_spinner_item,getActivity().getDatabase().getProfileTableProvider().getProfiles()));
		theSpinner.setAdapter(getDataAdapter());
		setSelectedProfile(null);
	}

	private void setSelectedProfile(Profile theProfile) {
		selectedProfile = theProfile;
	}
	private Profile getSelectedProfile(){
		return selectedProfile;
	}
	@Override
	public void onClick(DialogInterface dialog, int which) {
		if(which==DialogInterface.BUTTON_POSITIVE){
			Profile theProfile = getSelectedProfile();
			getActivity().getDatabase().getProfileTableProvider().deleteProfileById(theProfile.getId());
			refreshDataAdapter();
		}
	}

	@Override
	public void onItemSelected(AdapterView<?> theParent, View theView, int thePosition,
			long theId) {
		setSelectedProfile(getDataAdapter().getItem(thePosition));
	}

	@Override
	public void onNothingSelected(AdapterView<?> theParent) {
		setSelectedProfile(null);
	}

}
